package com.threeti.jiayusms.infrastructure.legacy;

import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.springframework.util.CollectionUtils.isEmpty;

import java.util.Collections;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.threeti.jiayusms.domain.model.legacy.Order;
import com.threeti.jiayusms.framework.dao.AbstractCustomKeyDao;
import com.threeti.jiayusms.framework.dao.EntityAlreadyExistException;
import com.threeti.jiayusms.framework.dao.EntitySaveOrUpdateException;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.interfaces.OrderDynaCriteria;

@Repository
public class OrderDaoImpl extends AbstractCustomKeyDao<String, Order> implements OrderDao {

	@Autowired
	public OrderDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	protected DetachedCriteria buildDefaultCriteria() {
		return DetachedCriteria.forClass(Order.class);
	}
	
	@Override
	public PagedListHandler<Order> findAllOrdersByPage(int currentPageIndex,
			int startingIndex) {
		PagedListHandler<Order> pagedListHandler = this.findAllByPage(currentPageIndex, startingIndex);
	    List<Order> pagedList = pagedListHandler.getPageList();
	    if (!isEmpty(pagedList))  return pagedListHandler;

	    return PagedListHandler.EMPTY_LIST;
	}

	@Override
	public Order loadBy(String customId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeBy(String customId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Order> findAll() {
		DetachedCriteria criteria = buildDefaultCriteria();
		List<Order> orderlist = this.findAllBy(criteria);
		return orderlist;
	}

	@Override
	public Order loadBy(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void remove(Order domainEntity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeBy(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Order store(Order domainEntity) throws EntityAlreadyExistException,
			EntitySaveOrUpdateException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> findOrderTemplatesByList(
			DynaSearchCriteria dynaSearchCriteria) {
	    OrderDynaCriteria dynaCriteria = (OrderDynaCriteria)dynaSearchCriteria;

		//模糊查询
		DetachedCriteria criteria = buildDefaultCriteria();
		
		String cargocount = dynaCriteria.getCargocount();
		if(isNotEmpty(cargocount)){
			criteria.add(Restrictions.eq("this.getCargocount", cargocount));
		}
		String cargoname = dynaCriteria.getCargoname();
		if(isNotEmpty(cargoname)){
			criteria.add(Restrictions.eq("this.getCargoname", cargoname));
		}
		String cargono = dynaCriteria.getCargono();
		if(isNotEmpty(cargono)){
			criteria.add(Restrictions.eq("this.getCargono", cargono));
		}
		String orderid = dynaCriteria.getOrderid();
		if(isNotEmpty(orderid)){
			criteria.add(Restrictions.eq("this.getOrderid", orderid));
		}

		List<Order>  allList = this.findAllBy(criteria);
    if (!isEmpty(allList))  return allList;

    return Collections.emptyList();
	}

}
